package com.test2015.Swing.render.jlistrender;
import javax.swing.*;

import java.awt.*;
import java.io.*;
import java.net.*;
/**
 * @author Hardneedl
 */
final class IconRenderer extends JFrame {
	public String getTitle() {
		return "ImageIcon Renderer";
	}

	static private final Dimension size = new Dimension(280, 440);

	public Dimension getPreferredSize() {
		return size;
	}

	public Dimension getMaximumSize() {
		return size;
	}

	public Dimension getMinimumSize() {
		return size;
	}

	public Dimension getSize() {
		return size;
	}

	private JList jlist;

	IconRenderer() throws HeadlessException {
		init();
		attachListeners();
		doLay();

		this.setLocationRelativeTo(null);
	}
    private void init() {
        jlist = new JList(new ImageListModel());
        jlist.setCellRenderer(new DefaultListCellRenderer(){
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
                if (value instanceof Icon) {
                    try {
                        setIcon((Icon) value);
                        setText("Icon");
                        setVerticalTextPosition(SwingConstants.BOTTOM );
                        setHorizontalTextPosition(SwingConstants.CENTER );
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
                else {
                    setText("???");
                }
                return c;
            }
        });
    }
    private void attachListeners() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void doLay() {
        Container container = getContentPane();
        container.add(new JScrollPane(jlist));
        pack();
        setVisible(true);
    }

	public static void main(String[] args) {
		new IconRenderer();
	}
}